package service

import (
	"encoding/json"
	"errors"
	"time"
	"zk2/globals"
	"zk2/internal/dao"
	"zk2/internal/dto"
)

type PersonServiceImpl interface {
	List(req dto.PersonReq) (*dto.PersonResp, error)
}
type PersonService struct {
}

func NewPersonService() *PersonService {
	return &PersonService{}
}
func (p *PersonService) List(req dto.PersonReq) (*dto.PersonResp, error) {
	personDao := dao.NewPersonDao()
	result, _ := globals.Rdb.Get(globals.Ctx, "personList").Result()
	list, err := personDao.List(req)
	if err != nil {
		return nil, errors.New("我的联系人列表查询失败")
	}
	if result == "" {
		marshal, _ := json.Marshal(list)
		globals.Rdb.Set(globals.Ctx, "personList", marshal, time.Minute*10)
	} else {
		json.Unmarshal([]byte(result), &list)
	}
	return &dto.PersonResp{
		Persons: list,
	}, nil
}
